package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.ContractContain;
import com.asm.repository.ContractContainRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.ContractContainVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author michael
 */
@Service
public class ContractContainService {
    @Resource
    private ContractContainRepository contractContainRepository;
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取ContractContain的vo对象列表
     *
     * @param page 存储预定信息的对象
     * @return 存储了contractContainVo的列表对象
     */
    private List<ContractContainVo> getContractContainVoList(Page<ContractContain> page) {
        List<ContractContainVo> list = new ArrayList<>();
        for (ContractContain contractContain : page.getContent()) {
            ContractContainVo contractContainVo = new ContractContainVo();
            BeanUtils.copyProperties(contractContain, contractContainVo);
            list.add(contractContainVo);
        }
        return list;
    }

    /**
     * 根据编号查询合同包含
     *
     * @param id 编号
     * @return 合同包含
     */
    public ContractContainVo findContractContainById(Long id) {
        ContractContain contractContain = contractContainRepository.findContractContainById(id);
        ContractContainVo contractContainVo = new ContractContainVo();
        BeanUtils.copyProperties(contractContain, contractContainVo);
        return contractContainVo;
    }

    /**
     * 查找所有的ContractContain，并返回
     *
     * @param pageNumber 页码
     * @param pageSize   页大小
     * @return 存储了信息的列表对象
     */
    public PageResult<ContractContainVo> findAllContractContains(Integer pageNumber, Integer pageSize) {
        Page<ContractContain> page = contractContainRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), getContractContainVoList(page));
    }

    /**
     * 根据合同编号，查询相关ContractContain信息
     *
     * @param contractId 合同编号
     * @param pageNumber 页码
     * @param pageSize   页大小
     * @return 用于实现分页效果的对象
     */
    public PageResult<ContractContainVo> findContractContainByContractId(Long contractId, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<ContractContain> contractContains = contractContainRepository.findByContractId(contractId, page);
        return new PageResult<>(pageNumber, pageSize, contractContains.getTotalPages(), getContractContainVoList(contractContains));
    }

    /**
     * 根据合广告位id分页查询合同包含
     *
     * @param id 广告位编号
     * @return 合同包含
     */
    public PageResult<ContractContainVo> findContractContainBySpotId(Long id, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<ContractContain> contractContains = contractContainRepository.findBySpotId(id, page);
        return new PageResult<>(pageNumber, pageSize, contractContains.getTotalPages(), getContractContainVoList(contractContains));
    }

    /**
     * 添加指定的contractContain信息
     *
     * @param contractContainVo 需要添加的vo对象
     * @return 返回contractContain的id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long addContractContain(ContractContainVo contractContainVo) {
        ContractContain contractContain = new ContractContain();
        BeanUtils.copyProperties(contractContainVo, contractContain);
        contractContain.setId(snowflakeIdWorker.nextId());
        contractContain.setCreateAt(new Date());
        contractContain.setCreateBy(1L);
        contractContain.setUpdateAt(new Date());
        contractContain.setUpdateBy(1L);
        contractContainRepository.save(contractContain);
        return contractContain.getId();
    }

    /**
     * 更新指定的contractContain信息
     *
     * @param contractContainVo 需要更新的vo对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateContractContain(ContractContainVo contractContainVo) {
        ContractContain contractContain = contractContainRepository.findById(contractContainVo.getId()).get();
        BeanUtils.copyProperties(contractContainVo, contractContain);
        contractContain.setUpdateBy(1L);
        contractContain.setUpdateAt(new Date());
        contractContainRepository.save(contractContain);
    }

    /**
     * 删除指定的contractContain信息
     *
     * @param id contractContain的id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractContain(Long id) {
        ContractContain contractContain = contractContainRepository.findById(id).get();
        contractContain.setIsDel(true);
        contractContain.setUpdateBy(1L);
        contractContain.setUpdateAt(new Date());
        contractContainRepository.save(contractContain);
    }

}
